package com.example.demo.rx;

import com.example.demo.rx.cat.*;

import java.net.URL;
import java.util.Collections;
import java.util.List;

public class CatsHelper3Job3FuncFlatmap {
    ApiWrapper3Job apiWrapper;
    public AsyncJob<URL> saveTheCutest(String query) {
//        List<Cat> cats = api.queryCat(query);
//        Cat cutest = findCutest(cats);
//        URL saveUrl = api.store(cutest);
//        return saveUrl;
        var catsListJob = apiWrapper.queryCats(query);
        var cutestCatJob = catsListJob.map(this::findCutest);
        var storeURLJob = cutestCatJob.flatMap(apiWrapper::store);
        return storeURLJob;
    }

    private Cat findCutest(List<Cat> cats) {
        return Collections.max(cats);
    }
}
